package se.webbzon.boltzmann.minigame.botany;

import java.awt.Color;
import java.awt.Image;

import se.webbzon.boltzmann.minigame.effect.SandBoxBubble;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;

public class AnimalCell extends SandBoxCell {
	
	private static final double oxygenDrain = 1.0 / 2000.0;
	private static final double sugarDrain = 1.0 / 4000.0;
	
	// The oxygen and sugar level of this animal cell
	private final CellBar oxygen, sugar;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AnimalCell() {
		this(1,1);
	}
	
	public AnimalCell(double oxygenLevel, double sugarLevel) {
		AbstractCell cell = getInstance();
		oxygen = new CellBar(Color.red, oxygenLevel);
		sugar = new CellBar(Color.white, sugarLevel);
		cell.addBar(oxygen);
		cell.addBar(sugar);
	}

	/*============================================================
	Protected Methods
	============================================================*/
	
	@Override protected ResourceSet<Image> getImage() {
		return Resources.getImageSet("minigame_botany_animal_cell");
	}

	@Override protected boolean isHealthy() {
		return oxygen.getValue() > 0 && sugar.getValue() > 0;
	}

	@Override protected boolean canSplit() {
		return oxygen.getValue() > 0.5 && sugar.getValue() > 0.5;
	}

	@Override protected void onConsume() {
		oxygen.setValue(oxygen.getValue() - oxygenDrain);
		sugar.setValue(sugar.getValue() - sugarDrain);
	}

	@Override protected boolean canReact() {
		return oxygen.getValue() > 0.3 && sugar.getValue() > 0.2;
	}

	@Override protected void onReact() {
		oxygen.setValue(oxygen.getValue() - 0.3);
		sugar.setValue(sugar.getValue() - 0.2);
		SandBoxMolecule m;
		SandBox box = getSandBox();
		if (box != null) for (int i = 0; i < 6; i++) {
			m = new WaterMolecule();
			m.setPosition(getX(), getY());
			box.addItem(m);
			m = new CarbonDioxideMolecule();
			m.setPosition(getX(), getY());
			box.addItem(m);
		}
	}

	@Override protected boolean consumeResource(SandBoxItem item) {
		if (item instanceof OxygenMolecule) {
			oxygen.setValue(oxygen.getValue() + 0.05);
			return true;
		} else if (item instanceof SugarMolecule) {
			sugar.setValue(sugar.getValue() + 0.2);
			return true;
		} else if (item instanceof AbstractMolecule) {
			oxygen.setValue(oxygen.getValue() - 0.1);
			sugar.setValue(sugar.getValue() - 0.1);
			return true;
		} else
			return false;
	}

	@Override protected void onSplit() {
		SandBox box = getSandBox();
		
		// Create new cells
		AnimalCell cell;
		for (int i = 0; i < 2; i++) {
			cell = new AnimalCell(oxygen.getValue() * 0.5 ,sugar.getValue() * 0.5);
			double sign = i == 0 ? -1 : 1;
			cell.setPosition(getX() + sign * getWidth() * 0.4, getY());
			box.addItem(cell);
		}
		
		// Remove this cell
		box.removeItem(this);
		
		// Create bubbles
		for (int i = 0; i < 16; i++) {
			SandBoxBubble bubble = new SandBoxBubble();
			bubble.setPosition(	getX() + getWidth() * (-0.5 + Math.random()), 
								getY() + getHeight() * 0.5 * (-0.5 + Math.random()));
			box.addItem(bubble);
		}
	}

}
